package com.lenovo.smarttraffic.ui.fragment.stage5;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.SensorData;
import com.lenovo.smarttraffic.database.SensorDataDB;
import com.lenovo.smarttraffic.ui.fragment.MyBaseFragment;
import com.lenovo.smarttraffic.util.HttpUtils;

import org.litepal.LitePal;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class BusFindFragment extends MyBaseFragment {

    private List<String> xTime = new ArrayList<>();

    private Context context;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);

    private LineChart lineChart;
    private List<Entry> entries = new ArrayList<>();
    private LineData lineData = new LineData();
    private LineDataSet dataSet;

    private Handler handler = new Handler();

    private int type = 0;

    public BusFindFragment setType(int type) {
        this.type = type;
        return this;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bus_find, container, false);
        lineChart = view.findViewById(R.id.line_chart);
        TextView title = view.findViewById(R.id.title);
        title.setText(getTitle());

        return view;
    }

    private String getTitle() {
        switch (type) {
            case 0:
                return "二氧化碳浓度";
            case 1:
                return "空气温度";
            default:
                return "空气湿度";
        }
    }

    private void initChart() {
        Description description = new Description();
        description.setEnabled(false);
        lineChart.setDescription(description);

        dataSet = new LineDataSet(entries, "");//将数据赋值到你的线条上
        dataSet.setColor(Color.BLACK);
        dataSet.setDrawCircleHole(false);
        dataSet.setDrawValues(false);
        dataSet.setDrawIcons(false);
        dataSet.setDrawCircles(false);
        dataSet.setLineWidth(2.5f);

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setTextSize(13);
        xAxis.setTextColor(Color.BLACK);
        xAxis.setLabelRotationAngle(-45);//x轴标签旋转45度
        xAxis.setAvoidFirstLastClipping(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//x轴显示在底下
        xAxis.setDrawGridLines(false);//隐藏x轴辅助线
        xAxis.setAxisLineWidth(2);
        //x轴格式转换
        xAxis.setValueFormatter((value, axis) -> xTime.get((int) value) + "");

        YAxis yAxis = lineChart.getAxisLeft();
        yAxis.setTextSize(15);
        yAxis.setTextColor(Color.BLACK);
        yAxis.setDrawGridLines(false);
        yAxis.setAxisLineWidth(2);

        lineChart.getAxisRight().setEnabled(false);//隐藏y轴辅助线
        lineChart.setHighlightPerTapEnabled(false);
        lineChart.setHighlightPerDragEnabled(false);
        lineChart.setDoubleTapToZoomEnabled(false);
        lineChart.setDragDecelerationEnabled(true);
        lineChart.setPinchZoom(true);

        lineData.addDataSet(dataSet);//每加一条就add一次
        lineChart.setData(lineData);//把线条设置给你的lineChart上

        runnable.run();
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            HttpUtils.post("get_all_sense", new HttpUtils().add("UserName", MainActivity.userName), response -> {
                SensorData sensorData = new Gson().fromJson(response.toString(), SensorData.class);
                xTime.add(simpleDateFormat.format(new Date()));
                entries.add(new Entry(entries.size(), getData(sensorData)));

                dataSet.notifyDataSetChanged();
                lineData.notifyDataChanged();
                lineChart.notifyDataSetChanged();
                lineChart.invalidate();
            });
            handler.postDelayed(runnable, 3000);
        }
    };

    private int getData(SensorData sensorData) {
        switch (type) {
            case 0:
                return sensorData.getCo2();
            case 1:
                return sensorData.getTemperature();
            case 2:
                return sensorData.getHumidity();
            default:
                return -1;
        }
    }

    @Override
    public void onViewFinished() {
        initChart();
    }

}
